#!/bin/bash

source /home/shell/util.sh

function install () {
    if [ "$(ostype)" == "Ubuntu" ]; then
        printInfo "导入GPG KEY，请稍后，可能速度会比较慢..."
        curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
        while [ true ]; do
            curl -sL https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/|grep 'data-os="ubuntu"'|grep -Po "data-release=\"(.*?)\">(.*?)<\/option>"|grep -v Debian|perl -pe "s/data-release=\"(.*?)\">(.*?)<\/option>/\1=\2/g"
            read -p "请选择Ubuntu系统版本[最接近]：" version
            if [[ -z "${version}" ]]; then
                printFail "请输入版本"
            else
                break;
            fi
        done
tee /etc/apt/sources.list.d/gitlab-ce.list<<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu ${version} main
EOF
        sudo apt-get update
        sudo apt-get install gitlab-ce
    else
tee /etc/yum.repos.d/gitlab-ce.repo<<EOF
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el\$releasever/
gpgcheck=0
enabled=1
EOF
        sudo yum makecache
        sudo yum install gitlab-ce -y
    fi
}

function config () {
    read -p "GitLab端口[80]：" http_port
    http_port=${http_port:-"80"}

    sed -ri "s@external_url 'http://gitlab.example.com'@external_url 'http://"$host_addr":"$http_port"'@g" /etc/gitlab/gitlab.rb
    sed -ri "s/# gitlab_rails\['time_zone'\] = 'UTC'/gitlab_rails\['time_zone'\] = 'Asia\/Shanghai'/g" /etc/gitlab/gitlab.rb
    sed -ri "s/# gitlab_rails\['gitlab_shell_ssh_port'\] = 22/gitlab_rails\['gitlab_shell_ssh_port'\] = 22/g" /etc/gitlab/gitlab.rb

    gitlab-ctl reconfigure && gitlab-ctl restart
    /opt/gitlab/bin/gitlab-rake cache:clear

    firewall-addport $http_port

    while [ true ]; do
        if [ -f "/var/opt/gitlab/postgresql/data/postgresql.conf" ]; then
            sed -ri "s/listen_addresses = ''/listen_addresses = '\*'/g" /var/opt/gitlab/postgresql/data/postgresql.conf
            break;
        fi
        sleep 1s
        loadMsg "postgresql.conf配置中...";
    done
    
    loadmsg_time=0;
    while [ true ]; do
        if [ -f "/var/opt/gitlab/postgresql/data/pg_hba.conf" ]; then
tee /var/opt/gitlab/postgresql/data/pg_hba.conf -a<<EOF
local   all         all                               trust
host    all         all                               127.0.0.1/32 trust
EOF
            break;
        fi
        sleep 1s
        loadMsg "pg_hba.conf配置中...";
    done
    gitlab-ctl restart postgresql

sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql -h 127.0.0.1 gitlabhq_production<<EOF
ALTER USER gitlab WITH SUPERUSER;
\q
EOF
    

    printInfo "=========================gitlab========================="
    printInfo "访问地址：http://$host_addr:$http_port/"
    printInfo "安装配置：/etc/gitlab/gitlab.rb"
    printInfo "运行配置：/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml"
    printInfo "初始账号：root"
    printInfo "初始密码：`cat /etc/gitlab/initial_root_password|grep "Password:"|perl -pe "s/^Password:\s+(.*)/\1/g"`"
    printInfo "备份目录：/var/opt/gitlab/backups"
    printInfo "=========================gitlab========================="
}

function backup () {
    gitlab-backup create
    cp -r /etc/gitlab/gitlab.rb /var/opt/gitlab/backups/gitlab.rb
    cp -r /etc/gitlab/gitlab-secrets.json /var/opt/gitlab/backups/gitlab-secrets.json
}



install && config